package com.cn.jdia.tas.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.jdia.tas.context.SpringContextUtils;
import com.cn.jdia.tas.model.T_attrecord;
import com.cn.jdia.tas.model.U_department;
import com.cn.jdia.tas.module.CheckInfoModule;
import com.cn.jdia.tas.module.DeptCheckModule;
import com.cn.jdia.tas.module.DetailCheckInfo;
import com.cn.jdia.tas.service.IAttrecordService;
import com.cn.jdia.tas.service.IDepartmentService;
import com.cn.jdia.tas.service.ITasAccessService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Controller
@RequestMapping("/control")
public class DeptRecordController {

	@Resource
	private IAttrecordService service = null;

	@Resource
	private ITasAccessService accessService = null;

	@Resource
	private IDepartmentService deptService = null;

	// 废弃方法
	@RequestMapping("/deptrecord")
	public String getDeptRecordData(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");
		Gson gson = new Gson();
		Map<String, String> pmap = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String start = pmap.get("start");
		String deptl1 = pmap.get("deptl1");
		String deptl2 = pmap.get("deptl2");

		System.out.println(start);
		System.out.println(deptl1 + " , " + deptl2);

		// 如果没有二级部门就是用1级，有二级使用二级
		String deptid = "";
		if (deptl2.equals("null")) {
			deptid = deptl1;
		} else {
			deptid = deptl2;
		}

		List<T_attrecord> attList = service.getDeptAttrecords(deptid, start);
		String name;
		String id;
		String date;
		String time;
		String type;
		String machine;

		List<CheckInfoModule> list = new ArrayList<CheckInfoModule>();

		for (T_attrecord att : attList) {
			name = att.getEmployee().getEmployeename();
			id = att.getEmployeeid();
			date = att.getCheckdate();
			time = att.getChecktime();
			type = att.getChecktype();
			machine = att.getAttmachinename();

			CheckInfoModule module = new CheckInfoModule();
			module.setName(name);
			module.setId(id);
			module.setDate(date);
			module.setTime(time);
			module.setType(type);
			module.setMachine(machine);

			list.add(module);
		}

		// 转换
		Map<String, DetailCheckInfo> map = new HashMap<String, DetailCheckInfo>();
		Iterator<CheckInfoModule> it = list.iterator();
		while (it.hasNext()) {
			CheckInfoModule info = it.next();
			String tname = info.getName();

			if (!map.keySet().contains(tname)) {
				DetailCheckInfo de = new DetailCheckInfo();
				de.addChild(info);
				map.put(tname, de);
			} else {
				DetailCheckInfo de = map.get(tname);
				de.addChild(info);
				map.put(tname, de);
			}
		}

		List<DeptCheckModule> ll = new ArrayList<DeptCheckModule>();
		for (String ttname : map.keySet()) {
			DetailCheckInfo info = map.get(ttname);

			String beg = info.getFirstCheckTime(start);
			String end = info.getLastCheckTime(start);

			// System.out.println(ttname + " "+info.getList().get(0).getId() +
			// " " + beg + " " + end);

			if (end == null || "".equals(end)) {
				if (beg.compareTo("14:00") > 0) {
					end = beg;
					beg = "";
				}
			}

			DeptCheckModule module = new DeptCheckModule();
			module.setName(ttname);
			module.setId(info.getList().get(0).getId());
			module.setBegTime(beg);
			module.setEndTime(end);

			ll.add(module);
		}

		String retData = gson.toJson(ll);
		System.out.println(retData);

		out.write(retData);
		out.flush();
		out.close();

		return null;
	}

	@RequestMapping("/alldeptrecord")
	public String getAllDeptRecord(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");
		System.out.println(data);
		Gson gson = new Gson();
		Map<String, String> pmap = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		System.out.println("-----getAllDeptRecord-----");
		String deptId = pmap.get("d_id");
		String date = pmap.get("date");
		 System.out.println(deptId + " " + date);

		Map<String, Object> retMap = new HashMap<>();

		// 需要区分是否含专业线
		List<U_department> subDeptInfo = deptService.getSubDeptInfo(deptId);
		// 无专业线
		if (subDeptInfo.size() == 0) {
			System.out.println("无专业线");

			List<T_attrecord> parentDeptRcd = service.getDeptAttrecords(deptId, date);
			List<String> deptSignedCount = new ArrayList<>();
			if (parentDeptRcd.size() != 0) {
				for (T_attrecord rcd : parentDeptRcd) {
					// System.out.println(rcd.getEmployee().getEmployeename()+rcd.getChecktime());
					if (!deptSignedCount.contains(rcd.getEmployee().getEmployeename())) {
						deptSignedCount.add(rcd.getEmployee().getEmployeename());
					}
				}
			}

			retMap.put("check_member", deptSignedCount.size());
			retMap.put("dept_member", accessService.getChildDeptMemberCount(deptId));
			retMap.put("major_count", new ArrayList<>());
			
			//暂时先写0
			retMap.put("outcount", 0);
		} else { // 含专业线情况
			System.out.println("含专业线");

			List<T_attrecord> parentDeptRcd = service.getParentDeptRcd(deptId, date);
			List<String> deptSignedCount = new ArrayList<>();
			if (parentDeptRcd.size() != 0) {
				for (T_attrecord rcd : parentDeptRcd) {
					// System.out.println(rcd.getEmployee().getEmployeename()+rcd.getChecktime());
					if (!deptSignedCount.contains(rcd.getEmployee().getEmployeename())) {
						deptSignedCount.add(rcd.getEmployee().getEmployeename());
					}
				}
			}
			// else {
			// System.out.println("no people");
			// }

			int parentDeptMemberCount = accessService.getParentDeptMemberCount(deptId);
			List<Map<String, Object>> fullLevel2DeptCount = service.getFullLevel2DeptCount(deptId, date);
			// System.out.println(fullLevel2DeptCount.toString());

			retMap.put("dept_member", parentDeptMemberCount);
			retMap.put("check_member", deptSignedCount.size());
			retMap.put("major_count", fullLevel2DeptCount);
			
			int parentOutcheckMemberCount = service.getParentOutcheckMemberCount(date, deptId);
			retMap.put("outcount", parentOutcheckMemberCount);
		}

		// 各个专业线签到人数统计
		// adeptid 专业线id
		// dcount 打卡人数
		// List<Map<String, Object>> querydLevel2DeptCount =
		// service.querydLevel2DeptCount(deptId, date);
		// System.out.println(querydLevel2DeptCount.toString());
		//
		// List<Map<String, Object>> majorCount = new ArrayList<>();
		// Map<String, Object> mmap = null;
		// // 专业线列表
		// // 判断是否为0 ，0 无专业线只有一级部门
		// List<U_department> subDeptInfo = deptService.getSubDeptInfo(deptId);
		// List<Integer> has = new ArrayList<>();
		// for (int i = 0; i < subDeptInfo.size(); i++) {
		// U_department dep = subDeptInfo.get(i);
		// for (Map<String, Object> tmap : querydLevel2DeptCount) {
		// if (tmap.get("adeptid").equals(dep.getDeptid())) {
		// System.out.println(dep.getDeptid() + dep.getDeptname() +
		// tmap.get("dcount"));
		// has.add(i);
		//
		// mmap = new HashMap<>();
		// mmap.put("mid", dep.getDeptid());
		// mmap.put("mname", dep.getDeptname());
		// mmap.put("mcount", tmap.get("dcount"));
		// majorCount.add(mmap);
		//
		// continue;
		// }
		// }
		// }
		//
		// for (int j = has.size() - 1; j >= 0; j--) {
		// subDeptInfo.remove((int) has.get(j));
		// }
		//
		// for (U_department dep : subDeptInfo) {
		// System.out.println(dep.getDeptid() + dep.getDeptname() + "0");
		// mmap = new HashMap<>();
		// mmap.put("mid", dep.getDeptid());
		// mmap.put("mname", dep.getDeptname());
		// mmap.put("mcount", 0);
		// majorCount.add(mmap);
		// }

		// for (Map<String, Object> tmp : fullLevel2DeptCount) {
		// System.out.println(tmp.get("dept1") + " " + tmp.get("memcount") + " "
		// + tmp.get("ndeptname") + " "
		// + tmp.get("ncount"));
		// }

		out.write(gson.toJson(retMap));
		out.flush();
		out.close();

		return null;
	}

	@RequestMapping("/fulldeptrecord")
	public String getFullDeptRecordData(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");
		Gson gson = new Gson();
		Map<String, String> pmap = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		System.out.println("-----getFullDeptRecordData-----");

		String date2 = pmap.get("date");
		String deptid = pmap.get("d_id");

		// 需要区分是否含专业线
		List<U_department> subDeptInfo = deptService.getSubDeptInfo(deptid);
		List<T_attrecord> attList = null;
		if (subDeptInfo.size() == 0) {
			attList = service.getDeptAttrecords(deptid, date2);
		} else {
			attList = service.getFullDeptRcd(deptid, date2);
		}

		// List<T_attrecord> attList = service.getFullDeptRcd(deptid, date2);
		String name;
		String id;
		String date;
		String time;
		String type;
		String machine;

		List<CheckInfoModule> list = new ArrayList<CheckInfoModule>();

		for (T_attrecord att : attList) {
			name = att.getEmployee().getEmployeename();
			id = att.getEmployeeid();
			date = att.getCheckdate();
			time = att.getChecktime();
			type = att.getChecktype();
			machine = att.getAttmachinename();

			CheckInfoModule module = new CheckInfoModule();
			module.setName(name);
			module.setId(id);
			module.setDate(date);
			module.setTime(time);
			module.setType(type);
			module.setMachine(machine);

			list.add(module);
		}

		// 转换
		Map<String, DetailCheckInfo> map = new HashMap<String, DetailCheckInfo>();
		Iterator<CheckInfoModule> it = list.iterator();
		while (it.hasNext()) {
			CheckInfoModule info = it.next();
			String tname = info.getName();

			if (!map.keySet().contains(tname)) {
				DetailCheckInfo de = new DetailCheckInfo();
				de.addChild(info);
				map.put(tname, de);
			} else {
				DetailCheckInfo de = map.get(tname);
				de.addChild(info);
				map.put(tname, de);
			}
		}

		List<DeptCheckModule> ll = new ArrayList<DeptCheckModule>();
		for (String ttname : map.keySet()) {
			DetailCheckInfo info = map.get(ttname);

			String beg = info.getFirstCheckTime(date2);
			String end = info.getLastCheckTime(date2);

			// System.out.println(ttname + " "+info.getList().get(0).getId() +
			// " " + beg + " " + end);

			if (end == null || "".equals(end)) {
				if (beg.compareTo("14:00") > 0) {
					end = beg;
					beg = "";
				}
			}

			DeptCheckModule module = new DeptCheckModule();
			module.setName(ttname);
			module.setId(info.getList().get(0).getId());
			module.setBegTime(beg);
			module.setEndTime(end);

			ll.add(module);
		}

		String retData = gson.toJson(ll);
		// System.out.println(retData);

		out.write(retData);
		out.flush();
		out.close();

		return null;
	}
}
